package service

import (
	"day12_12_homework/config"
	"day12_12_homework/internal/models/dao"
	"day12_12_homework/pkg"
)

func CreateContent(userId, introId int, contents string) (int, string) {
	intro := dao.Content{
		UserId:   userId,
		IntroId:  introId,
		Contents: contents,
	}
	err := config.DB.Model(&dao.Content{}).Create(&intro).Error
	if err != nil {
		return 0, "评论失败"
	}
	return int(intro.ID), ""
}

func ListContents(introId int) []*dao.ContentList {
	var contents []*dao.Content
	config.DB.Model(&dao.Content{}).Where("intro_id=?", introId).Find(&contents)

	var sli []*dao.ContentList

	for _, c := range contents {

		time := pkg.DisposeTime(c.CreatedAt)

		contentList := dao.ContentList{
			UserId:   c.UserId,
			IntroId:  c.IntroId,
			Contents: c.Contents,
			Time:     time,
		}
		sli = append(sli, &contentList)
	}
	return sli
}
